package com.xyy.controller;

import com.xyy.common.PageResult;
import com.xyy.common.Result;
import com.xyy.dto.PushCommentDTO;
import com.xyy.dto.PushLikeDTO;

import com.xyy.service.UserPushService;
import com.xyy.util.JwtUtil;
import com.xyy.vo.PushCommentVO;
import com.xyy.vo.UserPushVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 用户端推文控制器
 */
@Slf4j
@RestController
@RequestMapping("/user/push")
@Validated
public class UserPushController {

    @Autowired
    private UserPushService userPushService;

    @Autowired
    private JwtUtil jwtUtil;



    /**
     * 获取所有推文列表（分页）
     */
    @GetMapping("/list")
    public Result<PageResult<UserPushVO>> getPushList(
            @RequestParam(defaultValue = "1") @Min(1) Integer current,
            @RequestParam(defaultValue = "10") @Min(1) Integer size,
            HttpServletRequest request) {
        try {
            // 从JWT中获取用户ID
            Long userId = jwtUtil.getUserIdFromRequest(request);
            
            PageResult<UserPushVO> result = userPushService.getPushList(current, size, userId);
            return Result.success("查询成功", result);
        } catch (Exception e) {
            log.error("查询推文列表失败", e);
            return Result.error(e.getMessage());
        }
    }

    /**
     * 根据ID查询推文详情
     */
    @GetMapping("/{pushId}")
    public Result<UserPushVO> getPushDetail(
            @PathVariable @NotNull(message = "推文ID不能为空") Long pushId,
            HttpServletRequest request) {
        try {
            // 从JWT中获取用户ID
            Long userId = jwtUtil.getUserIdFromRequest(request);
            
            UserPushVO result = userPushService.getPushById(pushId, userId);
            return Result.success("查询成功", result);
        } catch (Exception e) {
            log.error("查询推文详情失败，推文ID: {}", pushId, e);
            return Result.error(e.getMessage());
        }
    }

    /**
     * 点赞推文-
     */
    @PostMapping("/like")
    public Result<Void> likePush(@Valid @RequestBody PushLikeDTO likeDTO, HttpServletRequest request) {
        try {
            // 从JWT中获取用户ID
            Long userId = jwtUtil.getUserIdFromRequest(request);
            likeDTO.setUserId(userId);
            
            userPushService.likePush(likeDTO);
            return Result.success("点赞成功", null);
        } catch (Exception e) {
            log.error("点赞推文失败", e);
            return Result.error(e.getMessage());
        }
    }

    /**
     * 取消点赞推文
     */
    @PostMapping("/unlike")
    public Result<Void> unlikePush(@Valid @RequestBody PushLikeDTO likeDTO, HttpServletRequest request) {
        try {
            // 从JWT中获取用户ID
            Long userId = jwtUtil.getUserIdFromRequest(request);
            likeDTO.setUserId(userId);
            
            userPushService.unlikePush(likeDTO);
            return Result.success("取消点赞成功", null);
        } catch (Exception e) {
            log.error("取消点赞推文失败", e);
            return Result.error(e.getMessage());
        }
    }

    /**
     * 添加评论
     */
    @PostMapping("/comment")
    public Result<Void> addComment(@Valid @RequestBody PushCommentDTO commentDTO, HttpServletRequest request) {
        try {
            // 从JWT中获取用户ID
            Long userId = jwtUtil.getUserIdFromRequest(request);
            commentDTO.setUserId(userId);
            
            userPushService.addComment(commentDTO);
            return Result.success("评论成功", null);
        } catch (Exception e) {
            log.error("添加评论失败", e);
            return Result.error(e.getMessage());
        }
    }

    /**
     * 删除评论
     */
    @DeleteMapping("/comment/{commentId}")
    public Result<Void> deleteComment(
            @PathVariable @NotNull(message = "评论ID不能为空") Long commentId,
            HttpServletRequest request) {
        try {
            // 从JWT中获取用户ID
            Long userId = jwtUtil.getUserIdFromRequest(request);
            
            userPushService.deleteComment(commentId, userId);
            return Result.success("删除评论成功", null);
        } catch (Exception e) {
            log.error("删除评论失败，评论ID: {}", commentId, e);
            return Result.error(e.getMessage());
        }
    }

    /**
     * 分页查询推文评论
     */
    @GetMapping("/{pushId}/comments")
    public Result<PageResult<PushCommentVO>> getComments(
            @PathVariable @NotNull(message = "推文ID不能为空") Long pushId,
            @RequestParam(defaultValue = "1") @Min(1) Integer current,
            @RequestParam(defaultValue = "10") @Min(1) Integer size,
            HttpServletRequest request) {
        try {
            // 从JWT中获取用户ID
            Long userId = jwtUtil.getUserIdFromRequest(request);
            
            PageResult<PushCommentVO> result = userPushService.getComments(pushId, current, size, userId);
            return Result.success("查询成功", result);
        } catch (Exception e) {
            log.error("查询推文评论失败，推文ID: {}", pushId, e);
            return Result.error(e.getMessage());
        }
    }



    /**
     * 点赞评论
     */
    @PostMapping("/comment/{commentId}/like")
    public Result<Void> likeComment(
            @PathVariable @NotNull(message = "评论ID不能为空") Long commentId,
            HttpServletRequest request) {
        try {
            // 从JWT中获取用户ID
            Long userId = jwtUtil.getUserIdFromRequest(request);
            
            userPushService.likeComment(commentId, userId);
            return Result.success("点赞成功", null);
        } catch (Exception e) {
            log.error("点赞评论失败，评论ID: {}", commentId, e);
            return Result.error(e.getMessage());
        }
    }

    /**
     * 取消点赞评论
     */
    @PostMapping("/comment/{commentId}/unlike")
    public Result<Void> unlikeComment(
            @PathVariable @NotNull(message = "评论ID不能为空") Long commentId,
            HttpServletRequest request) {
        try {
            // 从JWT中获取用户ID
            Long userId = jwtUtil.getUserIdFromRequest(request);
            
            userPushService.unlikeComment(commentId, userId);
            return Result.success("取消点赞成功", null);
        } catch (Exception e) {
            log.error("取消点赞评论失败，评论ID: {}", commentId, e);
            return Result.error(e.getMessage());
        }
    }

    /**
     * 查询用户点赞的推文列表
     */
    @GetMapping("/liked")
    public Result<PageResult<UserPushVO>> getLikedPushes(
            @RequestParam(defaultValue = "1") @Min(1) Integer current,
            @RequestParam(defaultValue = "10") @Min(1) Integer size,
            HttpServletRequest request) {
        try {
            // 从JWT中获取用户ID
            Long userId = jwtUtil.getUserIdFromRequest(request);
            
            PageResult<UserPushVO> result = userPushService.getLikedPushes(userId, current, size);
            return Result.success("查询成功", result);
        } catch (Exception e) {
            log.error("查询用户点赞的推文列表失败", e);
            return Result.error(e.getMessage());
        }
    }
}
